<?php

use Symfony\Component\DomCrawler\Crawler;

/**
 * Parseur de documents
 *
 * @author Guy
 */
class Exploration extends Crawler{
    
    /**
     *
     * @var string[]
     */
    protected $sourcePaths;
    
    /**
     *
     * @var Documents[] 
     */
    protected $documents;
    
    public function __construct() {
        parent::__construct();
        
        $this->sourcePaths  = array();
        $this->documents    = array();
    }
    
    /**
     * Parse chaque document contenu dans la liste des sources fournies
     * @return \DocumentExplorer
     */
    public function parseDocuments()
    {
        $this->documents = array();
        
        //Parcours de chaque sources de données
        foreach ($this->sourcePaths as $sourcePath)
        {
            $paths = ls_files($sourcePath);
            
            //Parcours de chaque documents contenus dans le dossier source
            foreach($paths as $i => $path)
            {
                $text = \file_get_contents($path);
                $this->clear();
                $this->addContent($text, "HTML");

                $infos              = pathinfo($path);
                $document           = new Document();
                $this->documents[]  = $document->setPath($infos["basename"])
                        ->setTitre($this->getTitleDocument())
                        ->setIcone($this->getIconeDocument())
                        ->setDescription($this->getDescriptionDocument())
                        ->setContent($this->html());
                
//                if($i == 2)
//                {
//                    break;
//                }
            }
        }
        
        return $this->getDocuments();
    }
    
    protected function getDescriptionDocument()
    {
        return $this->getValueFromPriorities(array(
            "meta[name='description']" => "content"
        ));
    }
    
    protected function getIconeDocument()
    {
        return $this->getValueFromPriorities(array(
            "meta[property='og:image']" => "content",
            "link[rel='shortcut icon']" => "href"
        ));
    }
    
    protected function getTitleDocument()
    {
        $title = $this->filter("title");
        $titre = $title->count() ? trim($title->text()): null;

        return $titre;
    }
    
    protected function getValueFromPriorities($priorities)
    {
        foreach($priorities as $selector => $attr)
        {
            $currentIcone = $this->filter($selector);
            if($currentIcone->count())
            {
                return trim($currentIcone->attr($attr));
            }
        }
        
        return null;
    }
    
    public function addSourcePath($path)
    {
        $this->sourcePaths[] = $path;
        
        return $this;
    }
    
    public function getSourcePaths() {
        return $this->sourcePaths;
    }

    /**
     * 
     * @return Document[]
     */
    public function getDocuments() {
        return $this->documents;
    }
}
